package com.tentact.dao;

import com.tentact.vo.Employee;

import java.sql.SQLException;

public class Admindao {
    //登录功能 查询用户名和密码
    public Employee selectByEmployees(Employee employee) {
        String sql = "select username,userpwd,employeeid,employeename from employee where username=? and userpwd=?";
        Object[] parmas = {employee.getUsername(), employee.getUserpwd()};
        RowMapper<Employee> rm = (rs) -> {
            Employee employees = new Employee();
            try {
                employees.setUsername(rs.getString("username"));
                employees.setUserpwd(rs.getString("userpwd"));
                employees.setEmployeeid(rs.getInt("employeeid"));
                employees.setEmployeename(rs.getString("employeename"));
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            return employees;
        };
        return Dbutil.executeQuerySingle(sql, parmas, rm);

    }

    //修改密码
    public int updateByEmployees(String newuserpwd, String username, String userpwd) {
        String sql = "update employee set userpwd=? where username = ? and userpwd = ?";
        Object[] params = {newuserpwd, username, userpwd};
        return Dbutil.executeUpdate(sql, params);
    }

    //通过用户名查询密码
    public Employee selectByEmployeesUsername(String username) {
        String sql = "select userpwd from employee where username=?";
        Object[] parmas = {username};
        RowMapper<Employee> rm = (rs) -> {
            Employee employees = new Employee();

            try {
                employees.setUserpwd(rs.getString("userpwd"));
                employees.setUsername(rs.getString("username"));

                return employees;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

            return employees;
        };
        return Dbutil.executeQuerySingle(sql, parmas, rm);

    }


}
